/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.command;

import java.io.Serializable;
import java.util.List;

import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.core.command.invoker.CommandInvoker;
import org.openinsula.vulcano.orm.InitializingCallback;
import org.openinsula.vulcano.orm.command.factory.CommandFactory;
import org.openinsula.vulcano.orm.dao.Dao;
import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Template implementation that mimics {@link Dao} methods, just to ease common
 * operations and avoid the use of {@link CommandFactory}. It is up to the
 * developer to choose the preferred approach.
 * <p>
 * It is safe to use it as a singleton bean, since it is stateless.
 * <p>
 * I can't emphasize enough that <strong>each method invocation runs on a
 * different transaction</strong>. If you want to group some operations inside
 * a single transaction, then you <strong>must</strong> implement your own
 * {@link Command} and invoke it directly with {@link CommandInvoker}.
 * 
 * @author yanaga
 * @author rebola
 * @since 1.0
 * @see Command
 * @see CommandFactory
 * @see CommandInvoker
 * @see CommandOperations
 */
public class CommandTemplate implements CommandOperations {
	
	private CommandInvoker commandInvoker;
	
	public CommandTemplate() {
	}
	
	public CommandTemplate(final CommandInvoker commandInvoker) {
		this.commandInvoker = commandInvoker;
	}
	
	@Autowired
	public final void setCommandInvoker(final CommandInvoker commandInvoker) {
		this.commandInvoker = commandInvoker;
	}
	
	public final CommandInvoker getCommandInvoker() {
		return commandInvoker;
	}
	
	public <T> T findById(final Class<T> entityClass, final Serializable id) {
		return commandInvoker.invoke(CommandFactory.newFindById(entityClass, id));
	}

	public <T> T findById(final InitializingCallback<T> callback, final Class<T> entityClass, final Serializable id) {
		return commandInvoker.invoke(CommandFactory.newFindById(entityClass, id, callback));
	}

	public <T> T findUnique(final DaoQuery<T> daoQuery) {
		return commandInvoker.invoke(CommandFactory.<T> newFindUnique(daoQuery));
	}

	public <T> T findUnique(final InitializingCallback<T> callback, final DaoQuery<T> daoQuery) {
		return commandInvoker.invoke(CommandFactory.newFindUnique(daoQuery, callback));
	}

	public <T> List<T> find(final DaoQuery<T> daoQuery) {
		return commandInvoker.invoke(CommandFactory.<T> newFind(daoQuery));
	}

	public <T> List<T> find(final InitializingCallback<T> callback, final DaoQuery<T> daoQuery) {
		return commandInvoker.invoke(CommandFactory.<T> newFind(daoQuery, callback));
	}

	public void persist(final Object... entities) {
		commandInvoker.invoke(CommandFactory.newPersist(entities));
	}

	public void remove(final Object... entities) {
		commandInvoker.invoke(CommandFactory.newRemove(entities));
	}

	public void refresh(final Object... entities) {
		commandInvoker.invoke(CommandFactory.newRefresh(entities));
	}

	public <T> void refresh(final InitializingCallback<T> callback, final T... entities) {
		commandInvoker.invoke(CommandFactory.newRefresh(entities));
	}

}
